{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 文字识别\n",
    "\n",
    "百度百科:利用计算机自动识别字符的技术，是模式识别应用的一个重要领域。人们在生产和生活中，要处理大量的文字、报表和文本。为了减轻人们的劳动，提高处理效率，50年代开始探讨一般文字识别方法，并研制出光学字符识别器。60年代出现了采用磁性墨水和特殊字体的实用机器。60年代后期，出现了多种字体和手写体文字识别机，其识别精度和机器性能都基本上能满足要求。如用于信函分拣的手写体数字识别机和印刷体英文数字识别机。70年代主要研究文字识别的基本理论和研制高性能的文字识别机，并着重于汉字识别的研究。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始我们的实验\n",
    "(1)登录百度AI开发平台，选择“文字识别”，使用“创建应用”建立一个文字识别应用。\n",
    "获取应用的AppID、API Key、Secret Key，注意:Secret Key要先选择显示，然后复制其现实的字符。\n",
    "\n",
    "例如我创建的人脸识别应用获取的AppID、API Key、Secret Key如下：\n",
    "\n",
    "    \"\"\" 公开课版本的限定量文字识别，需要更改为自己的api接口 \"\"\"\n",
    "    word_APP_ID = '21161335'\n",
    "    word_API_KEY = 'h45HUETBcSNTHrDldK7wjGL7'\n",
    "    word_SECRET_KEY = 'GtnhMLMQ8e670ptptAvPzhbCBikwuoOL'\n",
    "\n",
    "百度AI开发平台注册链接:https://login.bce.baidu.com/?account=&redirect=http%3A%2F%2Fconsole.bce.baidu.com%2F%3Ffromai%3D1#/aip/overview\n",
    "\n",
    "(2)熟悉我封装的百度AI文字识别的模块\n",
    "在代码中输入如下代码，导入我的封装的模块\n",
    "\n",
    "    import sys\n",
    "    sys.path.append('../baidu_api_lib')\n",
    "    from baidu_picture import baidu_picture_2_msg\n",
    "    封装库提供两个方法：初始化方法，文字识别方法。\n",
    "\n",
    "<1>初始化方法，需要传入三个参数，就是上步我们创建“文字识别”应用获取的三个参数\n",
    "参数信息如下：\n",
    "\n",
    "    \"\"\" 公开课版本的限定量文字识别，需要更改为自己的api接口 \"\"\"\n",
    "    word_APP_ID = '21161335'\n",
    "    word_API_KEY = 'h45HUETBcSNTHrDldK7wjGL7'\n",
    "    word_SECRET_KEY = 'GtnhMLMQ8e670ptptAvPzhbCBikwuoOL'\n",
    "调用方式：\n",
    "\n",
    "    # 传入百度AI的参数\n",
    "    pic_msg = baidu_picture_2_msg(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "    初始化完毕后，获取pic_msg对象，操作该对象就能进行文字识别。\n",
    "\n",
    "<2>文字识别方法\n",
    "这是我封装百度文字识别方法的函数原型：\n",
    "\n",
    "    #从百度AI获取图片分析结果\n",
    "    response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg') \n",
    "\n",
    "pic_2_msg包含2个参数：\n",
    "\n",
    "baidu_request_url:百度AI的调用接口，\n",
    "   \n",
    "    #百度AI的调用url\n",
    "    baidu_request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "    \n",
    "    'read_word.jpg':进行人脸识别的图片。\n",
    "    返回值:response为文字识别的信息\n",
    "\n",
    "    response.json()[\"words_result\"]:识别结果数组\n",
    "    response.json()[\"words_result_num\"]:识别结果数，表示words_result的元素个数\n",
    "    response.json()[\"words_result\"][\"words\"]:文字\n",
    "    ...还有不少参数，通过print(response.json())可以发现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "######################################################\n",
    "#\n",
    "# 先感受下小派的过目不忘,选择本cell，按shirt+enter键运行本模块\n",
    "#\n",
    "######################################################\n",
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "#\n",
    "# Copyright (c) 2020 Taste all Pi.\n",
    "#\n",
    "# Licensed under the GNU General Public License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#   http://www.gnu.org/licenses/gpl-2.0.html\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "from multiprocessing import Process, Queue\n",
    "import multiprocessing\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课版本的限定量文字识别，需要更改为自己的api接口 \"\"\"\n",
    "word_APP_ID = '21161335'\n",
    "word_API_KEY = 'h45HUETBcSNTHrDldK7wjGL7'\n",
    "word_SECRET_KEY = 'GtnhMLMQ8e670ptptAvPzhbCBikwuoOL'\n",
    "\n",
    "#百度AI的调用url\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "\n",
    "#　获取摄像头图像\n",
    "def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "    \n",
    "    # 创建一个VideoCapture对象\n",
    "    capture = cv.VideoCapture(0) \n",
    "    \n",
    "    #更新人脸识别信息\n",
    "    mydict[\"get_pic\"] = \"false\"\n",
    "    \n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #将拍摄到的图片发送到消息队列中\n",
    "            if mydict[\"get_pic\"] == \"true\":\n",
    "                mydict[\"get_pic\"] = \"false\"\n",
    "                mult_queue1.put(frame)\n",
    "\n",
    "            # 本地显示视频图像\n",
    "            cv.imshow('real_time picture', frame) \n",
    "            cv.waitKey(1)\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()    \n",
    "        #关闭显示窗口\n",
    "        cv.destroyAllWindows() \n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "#处理图像\n",
    "def proc_frame_func(task_name, mult_queue, mydict):\n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    # 传入百度AI的参数\n",
    "    pic_msg = baidu_picture_2_msg(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "    \n",
    "    #传入百度AI的参数\n",
    "    word_2_sound = baidu_word_2_sound(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "    word_2_sound.trans_word_to_sound(\"咱们比拼下谁记得多，让我们开始吧\",'tst_sound.mp3')\n",
    "    os.system('mplayer ' + 'tst_sound.mp3')\n",
    "    time.sleep(2)\n",
    "    \n",
    "    try:\n",
    "       \n",
    "        while True:\n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"我正在速记文字\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(2)\n",
    "            word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "\n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #关闭显示窗口\n",
    "            cv.destroyAllWindows()            \n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"你记下来了吗?反正我记下来了，可以把书拿走了，我来背诵,你来检查\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            \n",
    "            # 写入图片\n",
    "            cv.imwrite('read_word.jpg',frame)\n",
    "            \n",
    "            #从百度AI获取图片分析结果\n",
    "            response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "            \n",
    "            #给出百度AI分析的数据\n",
    "            print(response.json())\n",
    "            \n",
    "            #获取分析结果中的有效文字列表\n",
    "            word_list = response.json()['words_result']\n",
    "\n",
    "            #将识别到的文字组合到一起，存储在all_word中\n",
    "            all_word = ''\n",
    "            for k in word_list:\n",
    "                all_word += k['words']\n",
    "                all_word += ','\n",
    "                \n",
    "            print(all_word)\n",
    "            \n",
    "            #背诵文章\n",
    "            if all_word:\n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(all_word,'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(\"我都背下来了吧，我都有点小骄傲\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            else:\n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(\"你在跟我开玩笑吗?不要晃动书，我会晕的\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"不服就翻下一页，我们继续比拼\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')    \n",
    "            time.sleep(3)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('tst_sound.mp3')\n",
    "        os.remove('read_word.jpg')\n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        \n",
    "        mydict=multiprocessing.Manager().dict()\n",
    "        \n",
    "        #　定义传递图像队列和传递图像处理结果队列\n",
    "        q_frame = Queue()\n",
    "        \n",
    "        #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "        get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "        proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "        \n",
    "        # 启动任务\n",
    "        get_camera_frame.start()\n",
    "        proc_frame.start()\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 让我们临摹代码，学习“文字识别”功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# #导入标准库\n",
    "# import sys\n",
    "\n",
    "# import os\n",
    "\n",
    "# from playsound import playsound\n",
    "\n",
    "# import cv2 as cv\n",
    "\n",
    "# import time\n",
    "\n",
    "# from multiprocessing import Process, Queue\n",
    "\n",
    "# import multiprocessing\n",
    "\n",
    "\n",
    "# #导入自定义库\n",
    "# sys.path.append('../baidu_api_lib')\n",
    "\n",
    "# from baidu_picture import baidu_picture_2_msg\n",
    "\n",
    "# from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\n",
    "# \"\"\" 公开课版本的限定量文字识别，需要更改为自己的api接口 \"\"\"\n",
    "# word_APP_ID = '21161335'\n",
    "\n",
    "# word_API_KEY = 'h45HUETBcSNTHrDldK7wjGL7'\n",
    "\n",
    "# word_SECRET_KEY = 'GtnhMLMQ8e670ptptAvPzhbCBikwuoOL'\n",
    "\n",
    "\n",
    "# #百度AI的调用url\n",
    "# baidu_request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "\n",
    "\n",
    "# #　获取摄像头图像\n",
    "# def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "\n",
    "    \n",
    "#     # 创建一个VideoCapture对象\n",
    "#     capture = cv.VideoCapture(0) \n",
    "\n",
    "    \n",
    "#     #更新人脸识别信息\n",
    "#     mydict[\"get_pic\"] = \"false\"\n",
    "\n",
    "    \n",
    "#     #　给出提示信息\n",
    "#     print(task_name + \"任务启动\")\n",
    "\n",
    "    \n",
    "#     try:\n",
    "\n",
    "#         while True:\n",
    "\n",
    "#             # 一帧一帧读取视频\n",
    "#             ret, frame = capture.read()\n",
    "\n",
    "            \n",
    "#             #将拍摄到的图片发送到消息队列中\n",
    "#             if mydict[\"get_pic\"] == \"true\":\n",
    "\n",
    "#                 mydict[\"get_pic\"] = \"false\"\n",
    "\n",
    "#                 mult_queue1.put(frame)\n",
    "\n",
    "\n",
    "#             # 本地显示视频图像\n",
    "#             cv.imshow('real_time picture', frame) \n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "\n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         # 释放cap,销毁窗口\n",
    "#         capture.release()   \n",
    "\n",
    "#         #关闭显示窗口\n",
    "#         cv.destroyAllWindows() \n",
    "\n",
    "#         print(task_name + \"任务被终止\")\n",
    "\n",
    "        \n",
    "# #处理图像\n",
    "# def proc_frame_func(task_name, mult_queue, mydict):\n",
    "\n",
    "#     #　给出提示信息\n",
    "#     print(task_name + \"任务启动\")\n",
    "\n",
    "    \n",
    "#     # 传入百度AI的参数\n",
    "#     pic_msg = baidu_picture_2_msg(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "\n",
    "    \n",
    "#     #传入百度AI的参数\n",
    "#     word_2_sound = baidu_word_2_sound(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "\n",
    "#     word_2_sound.trans_word_to_sound(\"咱们比拼下谁记得多，让我们开始吧\",'tst_sound.mp3')\n",
    "\n",
    "#     os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#     time.sleep(2)\n",
    "\n",
    "    \n",
    "#     try:\n",
    "       \n",
    "#         while True:\n",
    "\n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"我正在速记文字\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(2)\n",
    "\n",
    "#             word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "            \n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "\n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "#             time.sleep(1)\n",
    "\n",
    "            \n",
    "#             #从队列中获取图片，显示图像质量\n",
    "#             mydict[\"get_pic\"] = \"true\"\n",
    "\n",
    "#             frame = mult_queue.get()\n",
    "\n",
    "#             cv.waitKey(1)\n",
    "\n",
    "            \n",
    "#             #关闭显示窗口\n",
    "#             cv.destroyAllWindows()    \n",
    "\n",
    "            \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"你记下来了吗?反正我记下来了，可以把书拿走了，我来背诵,你来检查\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "            \n",
    "#             # 写入图片\n",
    "#             cv.imwrite('read_word.jpg',frame)\n",
    "\n",
    "            \n",
    "#             #从百度AI获取图片分析结果\n",
    "#             response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "\n",
    "            \n",
    "#             #给出百度AI分析的数据\n",
    "#             print(response.json())\n",
    "\n",
    "            \n",
    "#             #获取分析结果中的有效文字列表\n",
    "#             word_list = response.json()['words_result']\n",
    "\n",
    "\n",
    "#             #将识别到的文字组合到一起，存储在all_word中\n",
    "#             all_word = ''\n",
    "\n",
    "#             for k in word_list:\n",
    "\n",
    "#                 all_word += k['words']\n",
    "\n",
    "#                 all_word += ','\n",
    "\n",
    "                \n",
    "#             print(all_word)\n",
    "\n",
    "            \n",
    "#             #背诵文章\n",
    "#             if all_word:\n",
    "\n",
    "#                 #给出语音提示\n",
    "#                 word_2_sound.trans_word_to_sound(all_word,'tst_sound.mp3')\n",
    "\n",
    "#                 os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "                \n",
    "#                 #给出语音提示\n",
    "#                 word_2_sound.trans_word_to_sound(\"我都背下来了吧，我都有点小骄傲\",'tst_sound.mp3')\n",
    "\n",
    "#                 os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "                \n",
    "#             else:\n",
    "\n",
    "#                 #给出语音提示\n",
    "#                 word_2_sound.trans_word_to_sound(\"你在跟我开玩笑吗?不要晃动书，我会晕的\",'tst_sound.mp3')\n",
    "\n",
    "#                 os.system('mplayer ' + 'tst_sound.mp3')\n",
    "\n",
    "                \n",
    "#             #给出语音提示\n",
    "#             word_2_sound.trans_word_to_sound(\"不服就翻下一页，我们继续比拼\",'tst_sound.mp3')\n",
    "\n",
    "#             os.system('mplayer ' + 'tst_sound.mp3')    \n",
    "\n",
    "#             time.sleep(3)\n",
    "\n",
    "            \n",
    "#     except KeyboardInterrupt:\n",
    "\n",
    "#         os.remove('tst_sound.mp3')\n",
    "\n",
    "#         os.remove('read_word.jpg')\n",
    "\n",
    "#         print(task_name + \"任务被终止\")\n",
    "\n",
    "        \n",
    "# if __name__ == \"__main__\":\n",
    "\n",
    "#     try:\n",
    "\n",
    "        \n",
    "#         mydict=multiprocessing.Manager().dict()\n",
    "\n",
    "        \n",
    "#         #　定义传递图像队列和传递图像处理结果队列\n",
    "#         q_frame = Queue()\n",
    "\n",
    "        \n",
    "#         #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "#         get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "\n",
    "#         proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "\n",
    "        \n",
    "#         # 启动任务\n",
    "#         get_camera_frame.start()\n",
    "\n",
    "#         proc_frame.start()\n",
    "\n",
    "\n",
    "#     except KeyboardInterrupt:\n",
    "#         print(\"任务被终止了\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 课后练习\n",
    "１、记忆书上的课文对小派还是很容易的，让我使用尝试下我们手写的文章，也让小派过目不忘的背诵下"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入标准库\n",
    "import sys\n",
    "import os\n",
    "from playsound import playsound\n",
    "import cv2 as cv\n",
    "import time\n",
    "from multiprocessing import Process, Queue\n",
    "import multiprocessing\n",
    "\n",
    "#导入自定义库\n",
    "sys.path.append('../baidu_api_lib')\n",
    "from baidu_picture import baidu_picture_2_msg\n",
    "from baidu_sound import baidu_word_2_sound\n",
    "\n",
    "\"\"\" 公开课版本的限定量文字识别，需要更改为自己的api接口 \"\"\"\n",
    "word_APP_ID = '21161335'\n",
    "word_API_KEY = 'h45HUETBcSNTHrDldK7wjGL7'\n",
    "word_SECRET_KEY = 'GtnhMLMQ8e670ptptAvPzhbCBikwuoOL'\n",
    "\n",
    "#百度AI的调用url 手写识别的百度AI接口\n",
    "baidu_request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "\n",
    "#　获取摄像头图像\n",
    "def camera_frame_func(task_name, mult_queue1, mydict):\n",
    "    \n",
    "    # 创建一个VideoCapture对象\n",
    "    capture = cv.VideoCapture(0) \n",
    "    \n",
    "    #更新人脸识别信息\n",
    "    mydict[\"get_pic\"] = \"false\"\n",
    "    \n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    try:\n",
    "        while True:\n",
    "            # 一帧一帧读取视频\n",
    "            ret, frame = capture.read()\n",
    "            \n",
    "            #将拍摄到的图片发送到消息队列中\n",
    "            if mydict[\"get_pic\"] == \"true\":\n",
    "                mydict[\"get_pic\"] = \"false\"\n",
    "                mult_queue1.put(frame)\n",
    "\n",
    "            # 本地显示视频图像\n",
    "            cv.imshow('real_time picture', frame) \n",
    "            cv.waitKey(1)\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        # 释放cap,销毁窗口\n",
    "        capture.release()    \n",
    "        #关闭显示窗口\n",
    "        cv.destroyAllWindows() \n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "#处理图像\n",
    "def proc_frame_func(task_name, mult_queue, mydict):\n",
    "    #　给出提示信息\n",
    "    print(task_name + \"任务启动\")\n",
    "    \n",
    "    # 传入百度AI的参数\n",
    "    pic_msg = baidu_picture_2_msg(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "    \n",
    "    #传入百度AI的参数\n",
    "    word_2_sound = baidu_word_2_sound(word_APP_ID, word_API_KEY, word_SECRET_KEY)\n",
    "    word_2_sound.trans_word_to_sound(\"咱们比拼下谁记得多，让我们开始吧\",'tst_sound.mp3')\n",
    "    os.system('mplayer ' + 'tst_sound.mp3')\n",
    "    time.sleep(2)\n",
    "    \n",
    "    try:\n",
    "       \n",
    "        while True:\n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"我正在速记文字\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(2)\n",
    "            word_2_sound.trans_word_to_sound(\"3\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"2\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "\n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"1\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            time.sleep(1)\n",
    "            \n",
    "            #从队列中获取图片，显示图像质量\n",
    "            mydict[\"get_pic\"] = \"true\"\n",
    "            frame = mult_queue.get()\n",
    "            cv.waitKey(1)\n",
    "            \n",
    "            #关闭显示窗口\n",
    "            cv.destroyAllWindows()            \n",
    "            \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"你记下来了吗?反正我记下来了，可以把书拿走了，我来背诵,你来检查\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')\n",
    "            \n",
    "            # 写入图片\n",
    "            cv.imwrite('read_word.jpg',frame)\n",
    "            \n",
    "            #从百度AI获取图片分析结果\n",
    "            response = pic_msg.pic_2_msg(baidu_request_url, 'read_word.jpg')\n",
    "            \n",
    "            #给出百度AI分析的数据\n",
    "            print(response.json())\n",
    "            \n",
    "            #获取分析结果中的有效文字列表\n",
    "            word_list = response.json()['words_result']\n",
    "\n",
    "            #将识别到的文字组合到一起，存储在all_word中\n",
    "            all_word = ''\n",
    "            for k in word_list:\n",
    "                all_word += k['words']\n",
    "                all_word += ','\n",
    "                \n",
    "            print(all_word)\n",
    "            \n",
    "            #背诵文章\n",
    "            if all_word:\n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(all_word,'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(\"我都背下来了吧，我都有点小骄傲\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            else:\n",
    "                #给出语音提示\n",
    "                word_2_sound.trans_word_to_sound(\"你在跟我开玩笑吗?不要晃动书，我会晕的\",'tst_sound.mp3')\n",
    "                os.system('mplayer ' + 'tst_sound.mp3')\n",
    "                \n",
    "            #给出语音提示\n",
    "            word_2_sound.trans_word_to_sound(\"不服就翻下一页，我们继续比拼\",'tst_sound.mp3')\n",
    "            os.system('mplayer ' + 'tst_sound.mp3')    \n",
    "            time.sleep(3)\n",
    "            \n",
    "    except KeyboardInterrupt:\n",
    "        os.remove('tst_sound.mp3')\n",
    "        os.remove('read_word.jpg')\n",
    "        print(task_name + \"任务被终止\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    try:\n",
    "        \n",
    "        mydict=multiprocessing.Manager().dict()\n",
    "        \n",
    "        #　定义传递图像队列和传递图像处理结果队列\n",
    "        q_frame = Queue()\n",
    "        \n",
    "        #　采集摄像头进程、处理图片进程、播报语音信息\n",
    "        get_camera_frame = Process(target=camera_frame_func, args=(\"获取摄像头图像\", q_frame, mydict))\n",
    "        proc_frame       = Process(target=proc_frame_func, args=(\"处理图像\", q_frame, mydict))\n",
    "        \n",
    "        # 启动任务\n",
    "        get_camera_frame.start()\n",
    "        proc_frame.start()\n",
    "\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"任务被终止了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
